package pers.dzk.fivelnarow.config;

import pers.dzk.fivelnarow.config.util.PieceSkin;
import pers.dzk.fivelnarow.config.util.PieceSkinResult;
import pers.dzk.fivelnarow.config.util.ResourcesPath;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 丁泽凯
 */
public class PieceConfig {
    private static Map<String, PieceSkinResult> skins = new HashMap();
    static {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(ResourcesPath.CONFIG_PATH+"/Piece"), StandardCharsets.UTF_8));
            String line;
            //noinspection AlibabaUndefineMagicConstant
            while (!(line = br.readLine()).trim().equals("#")) {
                String[] s = line.split(" ");
                PieceSkin bps = new PieceSkin();
                PieceSkin wps = new PieceSkin();
                bps.setImg(new ImageIcon(ResourcesPath.IMG_PATH+"/blackPiece/"+s[0]));
                System.out.print("黑棋地址："+ResourcesPath.IMG_PATH+"/blackPiece/"+s[0]);
                String[] s2 = s[1].split(",");
                bps.setOffset(new Point(Integer.parseInt(s2[0]),Integer.parseInt(s2[1])));
                System.out.print("  黑棋偏移量："+s2[0]+","+s2[1]);
                wps.setImg(new ImageIcon(ResourcesPath.IMG_PATH+"/whitePiece/"+s[2]));
                System.out.print("  白棋地址："+ResourcesPath.IMG_PATH+"/whitePiece/"+s[2]);
                String[] s3 = s[3].split(",");
                wps.setOffset(new Point(Integer.parseInt(s3[0]),Integer.parseInt(s3[1])));
                System.out.print("  黑棋偏移量："+s3[0]+","+s3[1]);
                PieceSkinResult psr = new PieceSkinResult();
                psr.setBlack(bps);
                psr.setWhite(wps);
                skins.put(s[4],psr);
                System.out.println("  皮肤名称："+s[4]);
            }
            br.close();
        } catch (NullPointerException | IOException e) {
            e.printStackTrace();
        }
        //遍历Piece初始化Map
    }
    public static PieceSkinResult getSkin(String name){
        return skins.get(name);
    }
    public static Map<String, PieceSkinResult> getSkins(){
        return skins;
    }

}
